home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / picedi1a / picedit.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-07-26  |  15.8 KB  |  510 lines

  1. VERSION 5.00
  2. Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "RICHTX32.OCX"
  3. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  4. Begin VB.Form frmPicEdit 
  5.    AutoRedraw      =   -1  'True
  6.    BackColor       =   &H80000004&
  7.    Caption         =   "Overlay text on picture"
  8.    ClientHeight    =   5205
  9.    ClientLeft      =   1515
  10.    ClientTop       =   3030
  11.    ClientWidth     =   5010
  12.    ClipControls    =   0   'False
  13.    BeginProperty Font 
  14.       Name            =   "MS Sans Serif"
  15.       Size            =   8.25
  16.       Charset         =   0
  17.       Weight          =   700
  18.       Underline       =   0   'False
  19.       Italic          =   0   'False
  20.       Strikethrough   =   0   'False
  21.    EndProperty
  22.    Icon            =   "PicEdit.frx":0000
  23.    LinkTopic       =   "Form1"
  24.    LockControls    =   -1  'True
  25.    OLEDropMode     =   1  'Manual
  26.    ScaleHeight     =   5205
  27.    ScaleWidth      =   5010
  28.    Begin MSComDlg.CommonDialog CommonDialog1 
  29.       Left            =   4080
  30.       Top             =   120
  31.       _ExtentX        =   847
  32.       _ExtentY        =   847
  33.       _Version        =   393216
  34.    End
  35.    Begin VB.CommandButton CmdOverlayText 
  36.       Height          =   405
  37.       Left            =   810
  38.       Picture         =   "PicEdit.frx":000C
  39.       Style           =   1  'Graphical
  40.       TabIndex        =   11
  41.       ToolTipText     =   "Proceed overlay"
  42.       Top             =   180
  43.       Visible         =   0   'False
  44.       Width           =   405
  45.    End
  46.    Begin VB.CommandButton cmdTextFont 
  47.       Height          =   405
  48.       Left            =   1440
  49.       Picture         =   "PicEdit.frx":010E
  50.       Style           =   1  'Graphical
  51.       TabIndex        =   1
  52.       ToolTipText     =   "Select text font"
  53.       Top             =   180
  54.       Width           =   405
  55.    End
  56.    Begin VB.CommandButton cmdTextColor 
  57.       Height          =   405
  58.       Left            =   2070
  59.       Picture         =   "PicEdit.frx":0908
  60.       Style           =   1  'Graphical
  61.       TabIndex        =   2
  62.       ToolTipText     =   "Select text color"
  63.       Top             =   180
  64.       Width           =   405
  65.    End
  66.    Begin VB.CommandButton cmdInputText 
  67.       Height          =   405
  68.       Left            =   810
  69.       Picture         =   "PicEdit.frx":0F72
  70.       Style           =   1  'Graphical
  71.       TabIndex        =   3
  72.       ToolTipText     =   "Input text"
  73.       Top             =   180
  74.       Width           =   405
  75.    End
  76.    Begin VB.CommandButton cmdClose 
  77.       Height          =   405
  78.       Left            =   3360
  79.       Picture         =   "PicEdit.frx":1074
  80.       Style           =   1  'Graphical
  81.       TabIndex        =   5
  82.       ToolTipText     =   "Close"
  83.       Top             =   180
  84.       Width           =   405
  85.    End
  86.    Begin VB.CommandButton cmdSave 
  87.       Height          =   405
  88.       Left            =   2730
  89.       Picture         =   "PicEdit.frx":186E
  90.       Style           =   1  'Graphical
  91.       TabIndex        =   4
  92.       ToolTipText     =   "Save"
  93.       Top             =   180
  94.       Width           =   405
  95.    End
  96.    Begin VB.CommandButton cmdOpen 
  97.       Height          =   405
  98.       Left            =   180
  99.       Picture         =   "PicEdit.frx":1ED8
  100.       Style           =   1  'Graphical
  101.       TabIndex        =   0
  102.       ToolTipText     =   "Open graphics file"
  103.       Top             =   180
  104.       Width           =   405
  105.    End
  106.    Begin VB.HScrollBar HScroll1 
  107.       Height          =   345
  108.       Left            =   0
  109.       TabIndex        =   8
  110.       Top             =   6360
  111.       Width           =   10755
  112.    End
  113.    Begin VB.PictureBox PicZ 
  114.       AutoRedraw      =   -1  'True
  115.       BackColor       =   &H80000006&
  116.       Height          =   3135
  117.       Left            =   180
  118.       ScaleHeight     =   205
  119.       ScaleMode       =   3  'Pixel
  120.       ScaleWidth      =   301
  121.       TabIndex        =   6
  122.       Top             =   1800
  123.       Width           =   4575
  124.       Begin RichTextLib.RichTextBox rtbText 
  125.          Height          =   465
  126.          Left            =   120
  127.          TabIndex        =   10
  128.          Top             =   2610
  129.          Visible         =   0   'False
  130.          Width           =   1815
  131.          _ExtentX        =   3201
  132.          _ExtentY        =   820
  133.          _Version        =   393217
  134.          BackColor       =   16777215
  135.          BorderStyle     =   0
  136.          Enabled         =   -1  'True
  137.          HideSelection   =   0   'False
  138.          Appearance      =   0
  139.          OLEDragMode     =   0
  140.          OLEDropMode     =   0
  141.          TextRTF         =   $"PicEdit.frx":1FDA
  142.       End
  143.       Begin VB.PictureBox PicX 
  144.          AutoRedraw      =   -1  'True
  145.          BorderStyle     =   0  'None
  146.          Height          =   2085
  147.          Left            =   0
  148.          ScaleHeight     =   2085
  149.          ScaleWidth      =   4035
  150.          TabIndex        =   9
  151.          Top             =   0
  152.          Width           =   4035
  153.       End
  154.       Begin VB.PictureBox PicY 
  155.          AutoRedraw      =   -1  'True
  156.          BackColor       =   &H8000000E&
  157.          BorderStyle     =   0  'None
  158.          Height          =   2580
  159.          Left            =   0
  160.          ScaleHeight     =   2580
  161.          ScaleWidth      =   4500
  162.          TabIndex        =   7
  163.          Top             =   0
  164.          Visible         =   0   'False
  165.          Width           =   4500
  166.       End
  167.    End
  168.    Begin VB.Label Label1 
  169.       Caption         =   "Label1"
  170.       Height          =   855
  171.       Left            =   180
  172.       TabIndex        =   12
  173.       Top             =   750
  174.       Width           =   4545
  175.    End
  176. Attribute VB_Name = "frmPicEdit"
  177. Attribute VB_GlobalNameSpace = False
  178. Attribute VB_Creatable = False
  179. Attribute VB_PredeclaredId = True
  180. Attribute VB_Exposed = False
  181. ' PicEdit.frm
  182. ' By Herman Liu
  183. ' To show how one can place rich text on graphics, in a simple way. (VB seperates
  184. ' rich text and picture as distinctly different types of format, and does not
  185. ' provide functions to allow superimposing the former on the latter). This code
  186. ' enables the user to directly input text (type on screen or through clipboard)
  187. ' at any point on picture, with options of text font size and color.
  188. Option Explicit
  189. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  190.      (ByVal hwnd As Long, ByVal msg As Long, ByVal wp As Long, Ip As Any) As Long
  191.      
  192. Private Type Rect
  193.     Left As Long
  194.     Top As Long
  195.     Right As Long
  196.     Bottom As Long
  197. End Type
  198. Private Type CharRange
  199.     firstChar As Long
  200.     lastChar As Long
  201. End Type
  202. Private Type FormatRange
  203.     hdc As Long
  204.     hdcTarget As Long
  205.     rectRegion As Rect
  206.     rectPage As Rect
  207.     mCharRange As CharRange
  208. End Type
  209. Private Const WM_USER As Long = &H400
  210. Private Const EM_FORMATRANGE As Long = WM_USER + 57
  211. Dim mFormatRange As FormatRange
  212. Dim rectDrawTo As Rect, rectPage As Rect
  213. Dim TextLength As Long, newStartPos As Long
  214. Dim dumpaway As Long
  215. Dim X1 As Single, Y1 As Single, X2 As Single, Y2 As Single
  216. Dim NoPicFlag As Boolean, RegionFlag As Boolean
  217. Dim fso As FileSystemObject
  218. Private Sub Form_Load()
  219.     Me.ScaleMode = vbTwips
  220.     PicX.ScaleMode = vbTwips
  221.     PicY.ScaleMode = vbTwips
  222.     PicZ.ScaleMode = vbPixels
  223.     PicZ.AutoSize = True
  224.     PicX.AutoSize = True
  225.     PicY.AutoSize = True
  226.     PicZ.AutoRedraw = True
  227.     PicX.AutoRedraw = True
  228.     PicY.AutoRedraw = True
  229.     PicZ.Visible = True
  230.     PicX.Visible = True
  231.     PicY.Visible = False
  232.     PicZ.BorderStyle = 1
  233.     PicX.BorderStyle = 0
  234.     PicY.BorderStyle = 0
  235.     PicZ.BackColor = &H80000006
  236.     PicY.Top = PicX.Top
  237.     PicY.Left = PicX.Left
  238.     X1 = 0: Y1 = 0: X2 = 0: Y2 = 0
  239.     CmdOverlayText.Visible = False
  240.     Set fso = New FileSystemObject
  241.     rtbText.Visible = False
  242.     If fso.FileExists("\windows\clouds.bmp") Then
  243.          PicX.Picture = LoadPicture("\windows\clouds.bmp", vbCFBitmap)
  244.          NoPicFlag = False
  245.     Else
  246.          NoPicFlag = True
  247.     End If
  248.     PicY.Width = PicX.Width
  249.     PicY.Height = PicX.Height
  250.     PicY.Picture = PicX.Picture
  251.     PicY.Move PicX.Top, PicX.Left
  252.     Dim t
  253.     t = "Steps: 1. Drag left-mouse on picture to frame a rectangle area."
  254.     t = t & " 2. Click the second button to allow input of text in that"
  255.     t = t & " area. 3. Type in text (you may select font and color)."
  256.     t = t & " 4. Click the second button again."
  257.     Label1.Caption = t
  258.     RegionFlag = False
  259. End Sub
  260. Private Sub cmdInputText_Click()
  261.     On Error GoTo errhandler
  262.     RegionFlag = False
  263.     If X2 - X1 <= 100 Or Y2 - Y1 <= 100 Then
  264.          MsgBox "No text input region yet"
  265.          Exit Sub
  266.     End If
  267.     If CmdOverlayText.Visible = True Then
  268.          If Len(rtbText.Text) = 0 Then
  269.              MsgBox "No text input yet"
  270.              Exit Sub
  271.          End If
  272.          TextToPic
  273.          Exit Sub
  274.     End If
  275.     If Clipboard.GetFormat(vbCFText) = True Then
  276.          rtbText.Text = Clipboard.GetText
  277.     Else
  278.          rtbText.Text = "Type text here"
  279.     End If
  280.         
  281.     ValidateDraw
  282.     rtbText.Width = (X2 - X1) / Screen.TwipsPerPixelX + 2
  283.     rtbText.Height = (Y2 - Y1) / Screen.TwipsPerPixelY + 2
  284.     If rtbText.SelColor = vbWhite Then
  285.         rtbText.BackColor = vbBlue
  286.     Else
  287.         rtbText.BackColor = vbWhite
  288.     End If
  289.     cmdInputText.Visible = False
  290.     CmdOverlayText.Visible = True
  291.     rtbText.Visible = True
  292.     rtbText.Enabled = True
  293.     rtbText.Move (X1 / Screen.TwipsPerPixelX), (Y1 / Screen.TwipsPerPixelY)
  294.     rtbText.SetFocus
  295.     cmdInputText.Visible = False
  296.     CmdOverlayText.Visible = True
  297.     Exit Sub
  298. errhandler:
  299.     ErrMsgProc "mnuFileOverlayText_Click"
  300. End Sub
  301. Private Sub TextToPic()
  302.     RegionFlag = False
  303.     If Len(rtbText.Text) = 0 Then
  304.         Exit Sub
  305.     End If
  306.     rtbText.Visible = False
  307.     rtbText.Enabled = False
  308.     Overlaying
  309.     PicY.Picture = PicY.Image
  310.     PicX.Picture = PicY.Picture
  311.     rtbText.Text = ""
  312.     CmdOverlayText.Visible = False
  313.     cmdInputText.Visible = True
  314. End Sub
  315. Private Sub Overlaying()
  316.     On Error GoTo errhandler
  317.     DoEvents
  318.     Screen.MousePointer = vbHourglass
  319.     rectPage.Left = X1
  320.     rectPage.Top = Y1
  321.     rectPage.Right = X2
  322.     rectPage.Bottom = Y2
  323.     rectDrawTo.Left = rectPage.Left
  324.     rectDrawTo.Top = rectPage.Top
  325.     rectDrawTo.Right = rectPage.Right
  326.     rectDrawTo.Bottom = rectPage.Bottom
  327.     mFormatRange.hdc = PicY.hdc
  328.     mFormatRange.hdcTarget = PicY.hdc
  329.     newStartPos = 0
  330.     mFormatRange.rectRegion = rectDrawTo
  331.     mFormatRange.rectPage = rectPage
  332.     mFormatRange.mCharRange.firstChar = newStartPos
  333.     mFormatRange.mCharRange.lastChar = -1
  334.     TextLength = Len(rtbText.Text)
  335.     Do
  336.         newStartPos = SendMessage(rtbText.hwnd, EM_FORMATRANGE, True, mFormatRange)
  337.         If newStartPos >= TextLength Then
  338.             Exit Do
  339.         End If
  340.         mFormatRange.mCharRange.firstChar = newStartPos
  341.         mFormatRange.hdc = PicY.hdc
  342.         mFormatRange.hdcTarget = PicY.hdc
  343.         DoEvents
  344.     Loop
  345.     dumpaway = SendMessage(rtbText.hwnd, EM_FORMATRANGE, False, ByVal CLng(0))
  346.     Screen.MousePointer = vbDefault
  347.     Exit Sub
  348. errhandler:
  349.     Screen.MousePointer = vbDefault
  350.     ErrMsgProc "Overlaying"
  351. End Sub
  352. Private Sub Form_Unload(Cancel As Integer)
  353.      End
  354. End Sub
  355. Private Sub picx_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  356.     If Button = vbLeftButton Then
  357.          rtbText.Visible = False
  358.          CmdOverlayText.Visible = False
  359.          cmdInputText.Visible = True
  360.          RegionFlag = True
  361.          PicX.DrawMode = vbInvert
  362.          X1 = X: X2 = X: Y1 = Y: Y2 = Y
  363.          PicX.Cls
  364.          PicX.Line (X, Y)-(X, Y), , B
  365.     End If
  366. End Sub
  367. Private Sub picX_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  368.     If Not RegionFlag Then
  369.          Exit Sub
  370.     End If
  371.     PicX.Line (X1, Y1)-(X2, Y2), , B
  372.     X2 = X
  373.     Y2 = Y
  374.     PicX.Line (X1, Y1)-(X2, Y2), , B
  375. End Sub
  376. Private Sub picX_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  377.     If Not RegionFlag Then
  378.         Exit Sub
  379.     Else
  380.         RegionFlag = False
  381.         PicX.DrawMode = vbCopyPen
  382.     End If
  383. End Sub
  384. Private Sub ValidateDraw()
  385.     Dim tmp As Single
  386.     If X1 > X2 Then
  387.         tmp = X1
  388.         X1 = X2
  389.         X2 = tmp
  390.     End If
  391.     If Y1 > Y2 Then
  392.         tmp = Y1
  393.         Y1 = Y2
  394.         Y2 = tmp
  395.     End If
  396. End Sub
  397. Private Sub cmdOpen_Click()
  398.     On Error GoTo errhandler
  399.     Dim mfilespec As String
  400.     CommonDialog1.Flags = cdlOFNHideReadOnly
  401.     CommonDialog1.FileName = ""
  402.     CommonDialog1.Filter = ""
  403.     CommonDialog1.CancelError = True
  404. FileNameRetry:
  405.     CommonDialog1.ShowOpen
  406.     If CommonDialog1.FileName = "" Then
  407.         Exit Sub
  408.     End If
  409.         
  410.     If Not fso.FileExists(CommonDialog1.FileName) Then
  411.         GoTo FileNameRetry
  412.     End If
  413.     mfilespec = CommonDialog1.FileName
  414.     Screen.MousePointer = vbHourglass
  415.     PicX.Cls
  416.     PicY.Cls
  417.     PicX.AutoSize = True
  418.     PicY.AutoSize = True
  419.     PicX.Picture = LoadPicture(mfilespec)
  420.     PicY.Picture = PicX.Picture
  421.     PicX.AutoSize = False
  422.     PicY.AutoSize = False
  423.     PicY.Move PicX.Top, PicX.Left
  424.     rtbText.SelColor = vbBlack
  425.     rtbText.BackColor = vbWhite
  426.     NoPicFlag = False
  427.     Screen.MousePointer = vbDefault
  428.     Exit Sub
  429. errhandler:
  430.     PicX.AutoSize = False
  431.     PicY.AutoSize = False
  432.     Screen.MousePointer = vbDefault
  433.     If Err <> 32755 Then
  434.          ErrMsgProc "frmPicEdit LoadPicFile"
  435.     End If
  436. End Sub
  437. Private Sub cmdTextFont_Click()
  438.     On Error GoTo errhandler
  439.     CommonDialog1.CancelError = True
  440.     CommonDialog1.Flags = cdlCFBoth
  441.     CommonDialog1.FontName = Screen.ActiveForm.FontName
  442.     CommonDialog1.ShowFont
  443.     rtbText.SelStart = 0
  444.     rtbText.SelLength = Len(rtbText.Text)
  445.     rtbText.SelFontName = CommonDialog1.FontName
  446.     rtbText.SelFontSize = CommonDialog1.FontSize
  447.     Exit Sub
  448. errhandler:
  449.     If Err.Number <> 32755 Then
  450.         ErrMsgProc "mnuFileTextFont_click"
  451.     End If
  452. End Sub
  453. Private Sub cmdTextColor_Click()
  454.     On Error GoTo errhandler
  455.     CommonDialog1.CancelError = True
  456.     CommonDialog1.Flags = cdlCFBoth
  457.     CommonDialog1.Color = Screen.ActiveForm.ForeColor
  458.     CommonDialog1.ShowColor
  459.     rtbText.SelStart = 0
  460.     rtbText.SelLength = Len(rtbText.Text)
  461.     rtbText.SelColor = CommonDialog1.Color
  462.     If rtbText.SelColor = vbWhite Then
  463.         rtbText.BackColor = vbBlue
  464.     Else
  465.         rtbText.BackColor = vbWhite
  466.     End If
  467.     Exit Sub
  468. errhandler:
  469.     If Err.Number <> 32755 Then
  470.         ErrMsgProc "mnuFileTextColor"
  471.     End If
  472. End Sub
  473. Private Sub cmdOverlayText_Click()
  474.     cmdInputText_Click
  475. End Sub
  476. Private Sub cmdSave_Click()
  477.     If NoPicFlag Then
  478.          MsgBox "No picture loaded yet"
  479.          Exit Sub
  480.     End If
  481.     On Error GoTo errhandler
  482.     Dim mfilespec As String
  483.     With CommonDialog1
  484.         .FileName = mfilespec
  485.         .Flags = cdlOFNHideReadOnly
  486.         .ShowSave
  487.     End With
  488.     mfilespec = CommonDialog1.FileName
  489.     If fso.FileExists(mfilespec) Then
  490.          If MsgBox("File already exists.  Overwirte?", vbYesNo + vbQuestion) = vbNo Then
  491.               Exit Sub
  492.          End If
  493.     End If
  494.     Screen.MousePointer = vbHourglass
  495.     SavePicture PicX.Picture, mfilespec
  496.     Screen.MousePointer = vbDefault
  497.     Exit Sub
  498. errhandler:
  499.     Screen.MousePointer = vbDefault
  500.     If Err <> 32755 Then
  501.          ErrMsgProc "frmPicEdit mnuFileSave_Click"
  502.     End If
  503. End Sub
  504. Private Sub cmdClose_Click()
  505.     End
  506. End Sub
  507. Sub ErrMsgProc(mMsg As String)
  508.     MsgBox mMsg & vbCrLf & Err.Number & Space(5) & Err.Description
  509. End Sub
  510.